package com.futao.techsharingmq.kafka.basic.simple.interceptors;

import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;

import java.util.Map;

/**
 * @author futao@gmail.com
 * @date 2021/11/4
 */
@Slf4j
public class ProducerInterceptor implements org.apache.kafka.clients.producer.ProducerInterceptor<String, String> {
    @Override
    public ProducerRecord<String, String> onSend(ProducerRecord<String, String> record) {
        log.debug("消息发送之前拦截，记录日志,topic:{},partition:{},key:{},value:{},header:{}", record.topic(), record.partition(), record.key(), record.value(), record.headers());
        // return new ProducerRecord<>(record.topic(), record.partition(), record.key(), record.value(), record.headers());
        return record;
    }

    @Override
    public void onAcknowledgement(RecordMetadata metadata, Exception exception) {
        // 早于生产者的结果回调方法的触发
        log.info("{}", metadata, exception);
    }

    @Override
    public void close() {
        log.info("生产者拦截器销毁");
    }

    @Override
    public void configure(Map<String, ?> configs) {
        log.info("生产者拦截器配置:{}", configs);
    }
}
